package com.dchh.android.suverynew.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dchh.android.suverynew.base.SBaseActivity;
import com.dchh.android.suverynew.common.SPreferencesManager;
import com.dhcc.android.base.base.MSApplication;
import com.dhcc.android.base.common.bean.KeyValueBean;
import com.dhcc.android.base.common.view.MyActorPopWindow;
import com.dhcc.android.base.db.DBManagerUtil;
import com.dhcc.android.base.utils.Constants;
import com.dhcc.android.base.utils.FileUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 实割实测作物选择
 *
 * @author zhoudan
 * @date 2016年3月28日
 */
public class SGSCZwSelectActivity extends SBaseActivity {

    private RelativeLayout add;
    private ListView listview;
    private LinearLayout next;
    private List<Map<String, String>> zwlist = new ArrayList<Map<String, String>>();
    private SimpleAdapter adapter;


    private List<KeyValueBean> kvlist = new ArrayList<KeyValueBean>();
    private String root;
    private String taskName;
    private String dbPath;
    private DBManagerUtil dbManagerUtil;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yield_sgsc_zw_select);
    }

    MyActorPopWindow.OnSelectListener selectListener = new MyActorPopWindow.OnSelectListener() {

        @Override
        public void onSelect(List<Map<String, String>> results) {
        }

        @Override
        public void OnSingleSelect(Map<String, String> map) {
            if (!zwlist.contains(map)) {
                zwlist.add(map);
                adapter.notifyDataSetChanged();
            }
        }
    };


    /**
     * 保存选择，为恢复做准备
     */
    private void savaMark() {
        // 保存信息
        SPreferencesManager.saveValue(MSApplication.USERNAME, Constants.USERNAME);
        SPreferencesManager.saveValue(MSApplication.PASSWORD, Constants.PASSWORD);
        SPreferencesManager.saveValue(MSApplication.PERIOD_CODE, Constants.currentCycle.getCode());
        SPreferencesManager.saveValue(MSApplication.PERIOD_LABEL, Constants.currentCycle.getLabel());
        SPreferencesManager.saveValue(MSApplication.TABLE_CODE, Constants.currentTable.getCode());
        SPreferencesManager.saveValue(MSApplication.TABLE_LABEL, Constants.currentTable.getLabel());
        SPreferencesManager.saveValue(MSApplication.TASK, taskName);
        SPreferencesManager.saveValue(MSApplication.EXCEPTION_FLAG, false);
    }

    @Override
    protected void setTitle(TextView toolbar_title) {
        toolbar_title.setText("实割实测作物选择");
    }

    @Override
    protected void initView() {
        root = SPreferencesManager.getValue(MSApplication.TASK_SAVE_PATH, FileUtil.getSavePath());
        taskName = Constants.currentTask.getTitle();
        dbPath = root + "/" + taskName + "/DEMAND/DEMAND.db";
        dbManagerUtil = DBManagerUtil.getInstance(dbPath, this);
        savaMark();
        kvlist = dbManagerUtil.getZWDatas("DIC_ZW", new KeyValueBean("PID", "VALUE", "CLASSIFICATION"),
                "CLASSIFICATION='农作物（100）'", dbPath);
        listview = (ListView) findViewById(R.id.zw_list);
        adapter = new SimpleAdapter(this, zwlist, R.layout.yield_sgsc_zw_item, new String[]{"ZWMC"}, new int[]{R.id.zwmc});
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Constants.zwMap = zwlist.get(position);
                Intent intent = new Intent(SGSCZwSelectActivity.this, SGSCMapSelectActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
            }
        });

        next = (LinearLayout) findViewById(R.id.next_layout);
        next.setVisibility(View.GONE);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.sgscz_select_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.add_operate) {
            MyActorPopWindow pw = new MyActorPopWindow(SGSCZwSelectActivity.this, findViewById(R.id.contentArea), selectListener);
            pw.show(kvlist, zwlist, new KeyValueBean("ZWBH", "ZWMC"), true);
        }
        return true;
    }
}
